﻿// -----------------------------------------------------------------------
// <copyright file="StrongTypedCollectionConversionConsumer.cs" company="">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------

namespace Dhgms.TplHelper.Model.Consumer
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

    using Dhgms.TplHelper.Model.Info.TaskResult;

    /// <summary>
    /// Consumer for changing a strongly typed collection from one type to another
    /// </summary>
    public abstract class StrongTypedCollectionConversionConsumer<TSourceType, TTargetType> : ITaskDataResult<IList<TTargetType>>
    {
        /// <summary>
        /// Gets the data.
        /// </summary>
        public IList<TTargetType> Data { get; set; }

        /// <summary>
        /// Consumes the source collection
        /// </summary>
        /// <param name="sourceCollection">The source collection data to consume.</param>
        public long Consume(IList<TSourceType> sourceCollection)
        {
            this.Data = sourceCollection.Select(this.GetConvertedRow).ToList();
            return this.Data.Count;
        }

        /// <summary>
        /// Converts a row from one type to another
        /// </summary>
        /// <param name="sourceRow">The record to convert</param>
        /// <returns>The converted record</returns>
        protected abstract TTargetType GetConvertedRow(TSourceType sourceRow);
    }
}
